<template>
  <div class="login-box" :style="back">
    <div class="login-logo-box">
      <img :src="logoimg" alt />
    </div>
    <div class="l-input-box">
      <input type="text" class="my-input" placeholder="用户名" maxlength="11" v-model="form.phone" />
      <input type="password" class="my-input" placeholder="密码" v-model="form.code" />
<!--      <p class="fright">忘记密码?</p>-->
    </div>
    <div class="l-btn-box">
      <mu-button class="btn login-btn" @click="login">登录</mu-button>
    </div>
  </div>
</template>

<script>
import logoimg from "@/assets/image/logo.png";
import { Toast } from "vant";
import "vant/lib/toast/style/index";
import store from "store";
export default {
  data() {
    return {
      logoimg: logoimg,
      form: {
        phone: "",
        code: ""
      },
      back: {
        background: "url(" + require("../../assets/image/login.jpg") + ")",
        backgroundRepeat: "no-repeat",
        backgroundSize: "cover"
      }
    };
  },
    created(){
      this.form.phone=window.localStorage.getItem('phone')
      this.form.code=window.localStorage.getItem('code')
    },
  methods: {
    login() {
      if (!this.form.phone) {
        Toast("用户名不能为空!");
        return;
      }
      //  if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.form.phone)) {
      //   Toast("手机号码有误，请重填");
      //   return false;
      // }
       if (!this.form.code) {
        Toast("密码不能为空");
        return;
      }

     this.$api.Login(this.form.phone,this.form.code).then(res=>{
       console.log(res)
       if(res.data.message.code==0){
        Toast("登录成功");
          let token = res.data.data.token;
          this.$store.dispatch("setAdminUser", res.data.data.userInfo);
          window.localStorage.setItem("token", token);
          window.localStorage.setItem("phone", this.form.phone);
          window.localStorage.setItem("code", this.form.code);
          store.set('adminuser', { adminuser: res.data.data.userInfo })

         this.$router.push("/admin/index");
       }else{
          Toast(res.data.message.message);

         // this.form.phone='';
         // this.form.code='';

       }
     })



    }
  }
};
</script>

<style>
</style>
